import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ActivatedRoute, Params } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import {
    IEntityDtoOfGuid,
    PagedDesignDto,
    WorkFlowDesignServiceProxy
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { Paginator } from 'primeng/paginator';
import { Table } from 'primeng/table';
import { finalize } from 'rxjs/operators';
import { FlowDesignDetailModalComponent } from './flow-design-detail-modal.component';
import { FlowFromModalComponent } from './flow-from-modal.component';
import { FlowNodeModalComponent } from './flow-node-modal.component';

@Component({
    templateUrl: './work-flow-design.component.html',
    animations: [appModuleAnimation()]
})
export class WorkFlowDesignComponent extends AppComponentBase {

    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;
    @ViewChild('createOrEditModal',{static: true}) createOrEditModal:FlowDesignDetailModalComponent;
    @ViewChild('flowFormDetailModal',{static: true}) flowFormDetailModal:FlowFromModalComponent;
    @ViewChild('flowNodeModal',{static: true}) flowNodeModal:FlowNodeModalComponent;

    advancedFiltersAreShown = false;
    filterText = '';
    kind: number;
    workflowCategoryId?:string;
    selectedDatas: IEntityDtoOfGuid[] = [];
    
    constructor(
        injector: Injector,
        private _activatedRoute: ActivatedRoute,
        private _workFlowDesignServiceProxy:WorkFlowDesignServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this._activatedRoute.params.subscribe((params: Params) => {
            this.kind = Number(params['kind']);
        });
    }


    getListData(event?: LazyLoadEvent) {
        this.selectedDatas = [];
        if (this.primengTableHelper.shouldResetPaging(event)) {
            if (this.primengTableHelper.TryChangePage(this.paginator, 0)) {
                return;
            }
        }
        this.primengTableHelper.showLoadingIndicator();
        this._workFlowDesignServiceProxy.getPagedDesign(this.workflowCategoryId,this.filterText,
          this.primengTableHelper.getSorting(this.dataTable),
        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        this.primengTableHelper.getSkipCount(this.paginator, event)).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }

    delete(){
        if (!this.selectedDatas) return;
        let ids = this.selectedDatas.map(item=>item.id);
        if (ids.length == 0) return;
        this.message.confirm(this.l('DeleteWarningMessage'), this.l('AreYouSure'),
            (isConfirmed) => {
                if (!isConfirmed) return;
                this._workFlowDesignServiceProxy.deleteDesign(ids)
                    .subscribe(() => {
                        this.reloadPage();
                        this.notify.success(this.l('SuccessfullyDeleted'));
                    });
            })
    }

    reloadPage() {
        if (this.primengTableHelper.TryChangePage(this.paginator, this.paginator.getPage())) {
            return;
        }

        this.getListData(null);
    }

    createWorkFlowDesign(){
       this.createOrEditModal.show('');
    }

    editWorkFlowDesign(record:PagedDesignDto){
        this.createOrEditModal.show(record.id);
    }

    backToList(e){
        if (e){
            this.getListData();
        }
    }

    goFlowformDetail(record:PagedDesignDto){
        this.flowFormDetailModal.show(record.id);
    }

    goflowNodeModal(record:PagedDesignDto){
        this.flowNodeModal.show(record.id);
    }

}
